package com.zznzzn.myweb.config;

import com.zznzzn.myweb.Properties.MyProperties;
import com.zznzzn.myweb.interceptor.CORSInterceptor;
import com.zznzzn.myweb.interceptor.JWTInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private MyProperties properties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new JWTInterceptor()).
                addPathPatterns("/api/**").
                excludePathPatterns("/error").
                excludePathPatterns("/api/user/login").
                excludePathPatterns("/api/user/checkUname").
                excludePathPatterns("/api/user/newUser").
                excludePathPatterns("/api/item/allitems").
                excludePathPatterns("/api/item/listitem").
                excludePathPatterns("/api/uploads/**");

        registry.addInterceptor(new CORSInterceptor()).addPathPatterns("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        //增加静态资源访问

        registry.addResourceHandler("/api/uploads/**").addResourceLocations(properties.getUpload_file_location());
        registry.addResourceHandler("/api/pages/**").addResourceLocations("classpath:/static/");
    }

}
